package cn.jy.careuplusdoctor.http;

import java.util.HashMap;
import java.util.Map;

import cn.jy.careuplusdoctor.http.CareuHttpClient.ResponseHandler;
import cn.jy.careuplusdoctor.response.DoctorIdResponse;
import cn.jy.careuplusdoctor.response.DoctorRecsResponse;
import cn.jy.careuplusdoctor.response.HospitalResponse;
import cn.jy.careuplusdoctor.response.LoginResponse;
import cn.jy.careuplusdoctor.response.SendMassageResponse;
import cn.jy.careuplusdoctor.response.SickerResponse;
import cn.jy.careuplusdoctor.response.SubjectResponse;
import cn.jy.careuplusdoctor.response.VersionRecsResponse;
import cn.jy.careuplusdoctor.utils.JsonUtils;
import cn.jy.careuplusdoctor.utils.LogUtils;

public class HttpHelper {

	private static HttpHelper instance;
	public static HttpHelper getInstance()
	{
		if (instance == null) {
			instance = new HttpHelper();
		}
		return instance;
	}
	private HttpHelper(){}
	//登陆接口
	public void login(String username,String password,final onHttpListener<LoginResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("loginName", username);
		params.put("loginPassword", password);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_LOGIN, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				LoginResponse data = (LoginResponse) JsonUtils.getInstance().readValue(response, LoginResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void searchSicker(Map<String, String> params, final onHttpListener<SickerResponse> listener){
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_SICKER_LIST, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				SickerResponse data = (SickerResponse) JsonUtils.getInstance().readValue(response, SickerResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				// TODO Auto-generated method stub
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
			}
		});
	}
	
	public void resetPassword(String doctorId,String oldPassword,String newPassword,final onHttpListener<LoginResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("doctorId", doctorId);
		params.put("oldPassword", oldPassword);
		params.put("newPassword2", newPassword);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_RESET_PASSWORD, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				LoginResponse data = (LoginResponse) JsonUtils.getInstance().readValue(response, LoginResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void update(String currentVersion,String type,final onHttpListener<VersionRecsResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("currentVersion", currentVersion);
		params.put("type", type);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_UPDATE, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				VersionRecsResponse data = (VersionRecsResponse) JsonUtils.getInstance().readValue(response, VersionRecsResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void sendMessage(String token,String text,final onHttpListener<SendMassageResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("token", token);
		params.put("text", text);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_SEND_MESSAGE, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				SendMassageResponse data = (SendMassageResponse) JsonUtils.getInstance().readValue(response, SendMassageResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void reg(String phone,String password,String name,String sex,String licenceCode,String hid,String hospital,String sid,String subject,String medicalTitle,String email,String licencePicUrl ,final onHttpListener<LoginResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("phone", phone);
		params.put("password", password);
		params.put("dName", name);
		params.put("sex", sex);
		params.put("licenceCode", licenceCode);
		params.put("hid", hid);
		params.put("qHospital", hospital);
		params.put("sid", sid);
		params.put("qSubject", subject);
		params.put("medicalTitle", medicalTitle);
		params.put("email", email);
		params.put("licencePicUrl", licencePicUrl);
		
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_REG, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LoginResponse data = (LoginResponse) JsonUtils.getInstance().readValue(response, LoginResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void checkPhone(String phone,final onHttpListener<DoctorIdResponse> listener)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("phone", phone);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_CHECK_PHONE, params, new ResponseHandler() {
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				DoctorIdResponse data = (DoctorIdResponse) JsonUtils.getInstance().readValue(response,
						DoctorIdResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}

			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}

	public void recoverPassword(String doctorId, final onHttpListener<LoginResponse> listener) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("token", doctorId);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_FORGET_PASSWORD, params, new ResponseHandler() {
			@Override
			public void onSuccess(String response) {
				LoginResponse data = (LoginResponse) JsonUtils.getInstance().readValue(response, LoginResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}

			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	public void coctorInfo(String doctorId,final onHttpListener<DoctorRecsResponse> listener) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("doctorId", doctorId);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_DOCTOR_INFO, params, new ResponseHandler() {
			@Override
			public void onSuccess(String response) {
				DoctorRecsResponse data = (DoctorRecsResponse) JsonUtils.getInstance().readValue(response, DoctorRecsResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void doctorUpdate(String doctorId,String qHospital,String qSubject,String medicalTitle,String email,final onHttpListener<LoginResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("doctorId", doctorId);
		params.put("qHospital", qHospital);
		params.put("qSubject", qSubject);
		params.put("medicalTitle", medicalTitle);
		params.put("email", email);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_DOCTOR_UPDATE, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				LoginResponse data = (LoginResponse) JsonUtils.getInstance().readValue(response, LoginResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void getHospitalList(String Q_HName_S_LK,final onHttpListener<HospitalResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("Q_HName_S_LK", Q_HName_S_LK);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_GETHOSPITALLIST, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				HospitalResponse data = (HospitalResponse) JsonUtils.getInstance().readValue(response, HospitalResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	public void getHospitalSubjectList(String hid,final onHttpListener<SubjectResponse> listener)
	{
		Map<String, String> params = new HashMap<String,String>();
		params.put("hid", hid);
		CareuHttpClient.getIntance().postHttpByAsyncTask(HttpUrls.URL_GETHOSPITALSUBJECTLIST, params, new ResponseHandler() {
			
			@Override
			public void onSuccess(String response) {
				LogUtils.i("response", response);
				SubjectResponse data = (SubjectResponse) JsonUtils.getInstance().readValue(response, SubjectResponse.class);
				listener.onResponse(data, HttpUrls.REQUEST_SUCCESS);
			}
			
			@Override
			public void onFail(String response) {
				listener.onResponse(null, HttpUrls.REQUEST_FIAL);
				LogUtils.i("response", response);
			}
		});
	}
	
	
	public interface onHttpListener<V>{
		void onResponse(V data,int code);
	}
}
